package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.happypets.servicesImpl.UserServiceImpl;

/**
 * Servlet implementation class CommsDeal
 */
public class CommsDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommsDeal() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		UserServiceImpl psi = new UserServiceImpl();
		
//		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		List<Map<String,String>> lm = null;
		
		String flag = request.getParameter("flag");
		
		String id = request.getParameter("id");
		
		System.out.println(flag+"---"+id);
		
		if(flag=="1"||flag.equals("1")){
			
			//添加
			
			lm = new ArrayList<Map<String,String>>();
			
			
			String commect = request.getParameter("comment");
			String grade = request.getParameter("grade");
			
			String mag = psi.addComment(id, commect, grade)==1?"评论成功！":"评论失败！";
			
			Map<String,String> m = new HashMap<String,String>();
			
			m.put("msg", mag);
			
			lm.add(m);
		}else{
			
			lm = psi.userNoComms(id);
			
		}
		
		
		
		
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
